package com.tongji.showroom.service
{
	import com.tongji.showroom.events.AddImageEvent;
	import com.tongji.showroom.events.AddUserSceneEvent;
	import com.tongji.showroom.events.GetUserSceneEvent;
	import com.tongji.showroom.events.ReleaseProductEvent;
	import com.tongji.showroom.model.UserModel;
	
	import flash.system.Security;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.soap.WebService;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/08/
	 */ 
	public class UserSceneService extends Actor
	{
		private var ws:WebService;

		[Inject]
		public var model:UserModel;
		
		public function UserSceneService()
		{
			super();
			ws = new WebService();
			ws.wsdl = "http://share.rryjw.com/VRService.asmx?wsdl";
			ws.useProxy = false;
			ws.loadWSDL();
			Security.loadPolicyFile("http://img.rryjw.com/crossdomain.xml");
		}
		
		public function addUserScene(sceneStr:String):void
		{
			ws.AddUserScene(sceneStr);
			ws.AddUserScene.addEventListener(ResultEvent.RESULT, addUserSceneResult);
			ws.AddUserScene.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		/**获取用户保存的场景
		 * @author caddy
		 * @param userId
		 * @param currentPage
		 * @param pageSize
		 * 
		 */		
		public function getUserScene(userId:Number, currentPage:int, pageSize:int):void
		{
			ws.GetUserScene(userId, currentPage, pageSize);
			ws.GetUserScene.addEventListener(ResultEvent.RESULT, getUserSceneResult);
			ws.GetUserScene.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		public function releaseProduct(obj:Object):void
		{
			ws.UpdateUserSceneState(model.userID, obj.sid, 1);
			ws.UpdateUserSceneState.addEventListener(ResultEvent.RESULT, releaseProductResult);
			ws.UpdateUserSceneState.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		public function addImages(images:ArrayCollection):void
		{
			ws.AddImgList(images);
			ws.AddImgList.addEventListener(ResultEvent.RESULT, addImagesResult);
			ws.AddImgList.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		private function addUserSceneResult(event:ResultEvent):void
		{
			if (event.result)
			{
				Alert.show("保存成功");
				getUserScene(model.userID, 1, 6);
			}				
			else
				Alert.show("保存出现问题了");
		}
		
		private function getUserSceneResult(event:ResultEvent):void
		{
			dispatch(new GetUserSceneEvent(GetUserSceneEvent.UPDATE_USER_SCENE, 0, 0, 0, event.result as String));
		}
		
		private function addImagesResult(event:ResultEvent):void
		{
			dispatch(new AddImageEvent(AddImageEvent.UPDATE_IMAGE, event.result as ArrayCollection));
		}
		
		private function releaseProductResult(event:ResultEvent):void
		{
			if (event.result)
				Alert.show("恭喜您，发布成功");
			else
				Alert.show("对不起，您的发布出现问题了！");
		}
		
		private function faultHandler(event:FaultEvent):void
		{
			Alert.show("失败" + event.fault.toString());
		}
	}
}